package com.cnbot.cameratest;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Environment;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.cnbot.camera.ICameraBinder2;
import com.cnbot.camera.ICameraPreviewListener;
import com.cnbot.camera.utils.JavaFilter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity  implements ICameraPreviewListener {
    private final String TAG = MainActivity.class.getSimpleName();

    private ICameraBinder2 mCameraBinder;
    private int[] mCanvasRgb = new int[1280 * 720 * 3/2];
    private Bitmap bitmap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bindCameraService();
    }

    private void bindCameraService() {
        bindService(new Intent(MainActivity.this, DefaultCameraService.class), mServiceConnection, Service.BIND_AUTO_CREATE);

    }

    private final ServiceConnection mServiceConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mCameraBinder = (ICameraBinder2) service;
            mCameraBinder.resetParameters(new int[] { 1280, 720 }, Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
            mCameraBinder.addCameraPreviewListener(MainActivity.this);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };


    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {
        Log.d(TAG, "摄像头数据");
        JavaFilter.decodeYUV420SP(mCanvasRgb , data , 1280 , 720);
    }

    public void onPaiClick(View view) {
        try {
            Bitmap bit = Bitmap.createBitmap(mCanvasRgb, 1280, 720, Bitmap.Config.ARGB_8888);
            saveImage(bit);
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    private void unbindCameraService() {
        if (mCameraBinder != null) {
            mCameraBinder.removePreviewListener(this);
            mCameraBinder = null;
        }
        unbindService(mServiceConnection);

    }

    /** 保存方法 */
    public static void saveImage(Bitmap bmp) {
        File appDir = new File(Environment.getExternalStorageDirectory(), "Boohee");
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        String fileName = System.currentTimeMillis() + ".jpg";
        File file = new File(appDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindCameraService();
    }
}

